package com.lagou.demo.test;

import com.lagou.demo.dao.ClassesDao;
import com.lagou.demo.dao.ClassesDaoImp;
import com.lagou.demo.dao.LoginDao;
import com.lagou.demo.dao.LoginDaoImp;
import com.lagou.demo.pojo.Classes;
import com.lagou.demo.pojo.Page;
import com.lagou.demo.pojo.Student;
import com.lagou.demo.pojo.User;
import com.lagou.demo.service.ClassesService;
import com.lagou.demo.service.StudentService;
import com.lagou.demo.service.UserService;
import com.lagou.demo.util.DBUtil;
import com.lagou.demo.util.DruidUtil;
import org.junit.Test;

import java.sql.*;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class test {
    public static void main(String[] args) {
        System.out.println("hello world");
    }
    @Test
    public void test(){
        Connection connection =null;
        PreparedStatement preparedStatement =null;
        ResultSet resultSet =null;
        List<User> list = new ArrayList<>();
        try {
           connection = DruidUtil.getConnection();
           String sql="select * from t_user";
           preparedStatement = connection.prepareStatement(sql);
           resultSet = preparedStatement.executeQuery();
           while (resultSet.next()){
               User user = new User();
               user.setId(resultSet.getInt("id"));
               user.setUserName(resultSet.getString("username"));
               user.setPassword(resultSet.getString("password"));
               list.add(user);
           }
            for (User u: list) {
                System.out.println(u);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DruidUtil.close(connection,preparedStatement,resultSet);
        }

    }

    @Test
    public void test1(){
        Connection connection =null;
        PreparedStatement preparedStatement =null;
        int i=0;
        User user = new User("admin", "password");
        user.setId(1);
        try {
            connection = DruidUtil.getConnection();
            String sql="insert  into t_user (id,username,password)values(null,?,?)";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,user.getUserName());
            preparedStatement.setString(2,user.getPassword());
            i = preparedStatement.executeUpdate();
            if(i>0){
                System.out.println("添加成功！");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DruidUtil.close(connection,preparedStatement);
        }
    }
    @Test
    public void test2(){
        Connection connection =null;
        PreparedStatement preparedStatement =null;
        int i=0;
        User user = new User("rose", "123456");
        try {
            connection = DruidUtil.getConnection();
            String sql="delete from t_user where username= ?";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,user.getUserName());
            i = preparedStatement.executeUpdate();
            if(i>0){
                System.out.println("删除成功！");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DruidUtil.close(connection,preparedStatement);
        }
    }
    @Test
    public void test3(){
        Connection connection =null;
        PreparedStatement preparedStatement =null;
        int i=0;
        User user = new User("admin2", "654321");
        user.setId(4);
        try {
            connection = DruidUtil.getConnection();
            String sql="update t_user set password=? where username =? ";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,user.getPassword());
            preparedStatement.setString(2,user.getUserName());
            i = preparedStatement.executeUpdate();
            if(i>0){
                System.out.println("修改成功！");
            }else {
                System.out.println("修改失败！");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DruidUtil.close(connection,preparedStatement);
        }
    }
    @Test
    public void test4(){
        StudentService studentService = new StudentService();
        List<Student> students = studentService.StudentSearch(new Page(1, 10));
        for (Student s:students) {
            System.out.println(s);
        }
        Date date = new Date();
    }
    @Test
    public void test5(){
        Connection connection =null;
        PreparedStatement preparedStatement =null;
        ResultSet resultSet=null;
        Student student=new Student();
        List<Student> list=new ArrayList<>();
        try {
            connection = DruidUtil.getConnection();
            String sql="select * from t_student";
            preparedStatement = connection.prepareStatement(sql);
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()){
                int id = resultSet.getInt("id");
                String name = resultSet.getString("name");
                String sex = resultSet.getString("sex");
                java.sql.Date birthday = resultSet.getDate("birthday");
                String email = resultSet.getString("email");
                String comments = resultSet.getString("comments");
                int classNumber = resultSet.getInt("classNumber");
                student = new Student(id,name,sex,birthday,email,comments,classNumber);
                list.add(student);
            }
            System.out.println(list);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DruidUtil.close(connection,preparedStatement,resultSet);
        }
    }
    @Test
    public void test6(){
        try {
            Connection connection = DBUtil.getConnection();
            String sql="select * from t_user";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            ResultSet resultSet = preparedStatement.executeQuery();
            while (resultSet.next()){
                String username = resultSet.getString("username");
                String password = resultSet.getString("password");
                User user = new User(username, password);
                System.out.println(user);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {

        }
    }
    @Test
    public void tst(){
        Connection connection =null;
        PreparedStatement preparedStatement =null;
        ResultSet resultSet=null;
        Classes classes=new Classes();
        List<Classes> list=new ArrayList<>();
        try {
            connection = DruidUtil.getConnection();
            String sql="select * from classes";
            preparedStatement = connection.prepareStatement(sql);
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()){
                int id = resultSet.getInt("id");
                String className = resultSet.getString("className");
                String grade = resultSet.getString("grade");
                String teacherName = resultSet.getString("teacherName");
                String watchWord = resultSet.getString("watchWord");
                int number = resultSet.getInt("number");
                classes=new Classes(id,className,grade,teacherName,watchWord,number);
                list.add(classes);
            }
            System.out.println(list);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DruidUtil.close(connection,preparedStatement,resultSet);
        }
    }
    @Test
    public void test7(){
        ClassesService classes=new ClassesService();
        int i = classes.ClassAdd(new Classes("就业办","10","生的伟大","手动设",1));
        System.out.println(i);
    }
}
